//
//  GuidePageViewController.m
//  Tour
//
//  Created by 才诗琦 on 16/3/22.
//  Copyright © 2016年 张中烨. All rights reserved.
//

#import "GuidePageViewController.h"
#import "LYBgImageView.h"
#import "LYMovePathView.h"
#import "LYFireworksView.h"
#import "AppDelegate.h"
@interface GuidePageViewController ()
@property(nonatomic, assign)BOOL isOut;
@end

@implementation GuidePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.hidden = YES;
    
    [self setupGuide];
    
    
}

#pragma mark - 结束减速跳转主页
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    AppDelegate *app = [[UIApplication sharedApplication] delegate];
    
    //如果引导页要结束
    if (_isOut) {
        [UIView animateWithDuration:1.5 animations:^{
            scrollView.alpha = 0;
        } completion:^(BOOL finished) {
            //将scrollView移除
            [scrollView removeFromSuperview];
            
#pragma mark - 修改window的跟视图
            app.window.rootViewController = app.tabBar;
            [self presentViewController:[[HomepageViewController alloc]init] animated:YES completion:^{
                
            }];
        }];
    }
}

- (void)setupGuide
{
    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
    scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.bounds)*3, CGRectGetHeight(self.view.bounds));
    scrollView.delegate = self;
    scrollView.pagingEnabled = YES;
    scrollView.bounces = NO;
    
    
    for (int i=0; i<3; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.view.bounds)*i, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i+1]];
        [scrollView addSubview:imageView];
    }
    
    UIImageView *smallImageView = [[UIImageView alloc] initWithFrame:CGRectMake(80, CGRectGetHeight(self.view.bounds)/4, CGRectGetWidth(self.view.bounds)/2, CGRectGetHeight(self.view.bounds)/2)];
    smallImageView.userInteractionEnabled = YES;
    //    smallImageView.backgroundColor = [UIColor redColor];
    [self.view addSubview:smallImageView];
    
    smallScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(smallImageView.bounds), CGRectGetHeight(smallImageView.bounds))];
    smallImageView.center = scrollView.center;
    smallScrollView.scrollEnabled = NO;
    smallScrollView.delegate = self;
    smallScrollView.contentSize = CGSizeMake(CGRectGetWidth(smallImageView.bounds)*3, CGRectGetHeight(smallImageView.bounds));
    smallScrollView.pagingEnabled = YES;
    smallScrollView.bounces = NO;
    for (int i=0; i<3; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(smallImageView.bounds)*i, 0, CGRectGetWidth(smallImageView.bounds), CGRectGetHeight(smallImageView.bounds))];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i+1]];
        [smallScrollView addSubview:imageView];
    }
    
    UIButton *bu = [UIButton buttonWithType:UIButtonTypeCustom];
    bu.frame = CGRectMake(self.view.frame.size.width * 2 , self.view.frame.size.height - 50, 150, 50);
    [bu setTitle:@"立即进入" forState:UIControlStateNormal];
    [bu setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [bu addTarget:self action:@selector(firstpressed) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:bu];
    
    
    
    

    [smallImageView addSubview:smallScrollView];
    [self.view addSubview:scrollView];
    [self.view bringSubviewToFront:smallImageView];
    
    
}

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView{
    if (aScrollView == scrollView) {
        CGPoint point = scrollView.contentOffset;
        point.y = point.y*4;
        smallScrollView.contentOffset = CGPointMake(scrollView.contentOffset.x/2, scrollView.contentOffset.y);
    }
}
#warning 跳转按钮改变根视图
- (void)firstpressed
{
    
    UIApplication *app = [UIApplication sharedApplication];
    AppDelegate *app2 = app.delegate;
    app2.window.rootViewController = app2.tabBar;
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
